#!/bin/sh

if test x"$srcdir" != x""; then
    builddir="."	# running from make check, but it does not define that
else
    srcdir=`echo "$0" | sed s,[^/]*$,,`
    test "$srcdir" = "$0" && srcdir=.
    test -z "$srcdir" && srcdir=.
    builddir="$srcdir"	# running manually, have to assume
fi

srcdir=`cd $srcdir;pwd`
builddir=`cd $builddir;pwd`

testfile="$srcdir/tests"
if [ $# -ge 1 ]; then testfile="$1"; fi

dirs=`awk -F '#' '{print $1}' $testfile`

basedir=`head -n 1 $testfile | awk '{print $2}'`
cd "$srcdir"; if [ ! -d "$basedir" ]; then
    cd ..; if [ ! -d "$basedir" ]; then exit 77; fi	# for make distcheck
fi
basedir=`cd "$basedir";pwd`

cd "$builddir"
error=0

dtsdec="../src/dtsdec"; if [ ! -x "$dtsdec" ]; then
    if [ -x ../vc++/Release/dtsdec.exe ]; then
	dtsdec="../vc++/Release/dtsdec.exe"
	basedir=`echo "$basedir" | sed "s,/cygdrive/\(.\)/,\1:/,"`
    elif [ -x ../vc++/Debug/dtsdec.exe ]; then
	dtsdec="../vc++/Debug/dtsdec.exe"
	basedir=`echo "$basedir" | sed "s,/cygdrive/\(.\)/,\1:/,"`
    else
	echo "Can not find dtsdec executable"; exit 1
    fi
fi

for dir in $dirs; do
    echo $dir
    "$dtsdec" -co float "$basedir/$dir/stream" >/dev/null 2>&1 >output.float
    ./compare output.float "$basedir/$dir/output.float" || error=1
    rm -f output.float
done

exit $error
